package service

import (
	"errors"
	"perfect-admin/dao"
	"perfect-admin/model"
	"perfect-admin/model/request"
	"sort"
)

// 获取角色所有菜单列表
func GetRoleMenuTree(role_id int) (role_menu_tree []*model.SysBaseMenu, menu_path_map map[string]bool, err error) {
	var role_menu_list []*model.SysBaseMenu
	err = dao.DB.Model(&model.SysRole{RoleId: role_id}).Related(&role_menu_list, "SysBaseMenus").Error
	return GetMenuPathTree(role_menu_list)
}

func GetMenuPathTree(menu_list []*model.SysBaseMenu) (menu_tree []*model.SysBaseMenu, menu_path_map map[string]bool, err error) {
	// 构建角色菜单map 键为ID 值为菜单
	menu_map := make(map[uint]*model.SysBaseMenu)
	for _, menu := range menu_list {
		menu_map[menu.ID] = menu
	}
	// 顶级菜单id列表及为Children赋值
	var top_menu_sorts []int
	top_menu_order_map := make(map[int]uint)
	menu_path_map = make(map[string]bool)
	for _, menu := range menu_list {
		if menu.Path != "" {
			menu_path_map["/" + menu.Path] = true
		}
		if menu.Level > 0 {
			menu_map[menu.ParentId].Children = append(menu_map[menu.ParentId].Children, menu_map[menu.ID])
		} else {
			top_menu_sorts = append(top_menu_sorts, menu.Sort)
			top_menu_order_map[menu.Sort] = menu.ID
		}
	}
	sort.Ints(top_menu_sorts)
	for _, order := range top_menu_sorts {
		menu_tree = append(menu_tree, menu_map[top_menu_order_map[order]])
	}
	return
}

func GetAllMenuTree(request_type string) (role_menu_tree []*model.SysBaseMenu, sortList []int, err error) {
	if request_type == "1" {
		err = dao.DB.Where("level = ?", 0).Find(&role_menu_tree).Pluck("sort", &sortList).Error
		return
	}
	var role_menu_list []*model.SysBaseMenu
	if request_type == "2" {
		err = dao.DB.Where("level != ?", 2).Find(&role_menu_list).Error
	} else if request_type == "" || request_type == "3" {
		err = dao.DB.Find(&role_menu_list).Error
	} else {
		err = errors.New("请求参数有误")
		return
	}
	role_menu_tree, sortList, err = GetMenuTree(role_menu_list)
	return
}

func GetMenuTree(menu_list []*model.SysBaseMenu) (menu_tree []*model.SysBaseMenu, sortList []int, err error) {
	// 构建角色菜单map 键为ID 值为菜单
	menu_map := make(map[uint]*model.SysBaseMenu)
	for _, menu := range menu_list {
		menu_map[menu.ID] = menu
	}
	// 顶级菜单id列表及为Children赋值
	var top_menu_ids []uint
	for _, menu := range menu_list {
		if menu.Level > 0 {
			menu_map[menu.ParentId].Children = append(menu_map[menu.ParentId].Children, menu_map[menu.ID])
		} else {
			top_menu_ids = append(top_menu_ids, menu.ID)
			sortList = append(sortList, menu.Sort)
		}
	}
	for _, id := range top_menu_ids {
		menu_tree = append(menu_tree, menu_map[id])
	}
	return
}

func GetMenuList(queryInfo request.QueryInfo) (menus []model.SysBaseMenu, total int, err error) {
	if queryInfo.Query == "" {
		if err = dao.DB.Model(&model.SysBaseMenu{}).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&menus).Error
	} else {
		if err = dao.DB.Model(&model.SysBaseMenu{}).Where("name like ?", "%"+queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("name like ?", "%"+queryInfo.Query+"%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Find(&menus).Error
	}
	return
}

func AddMenu(menu model.SysBaseMenu) error {
	return dao.DB.Create(&menu).Error
}

func GetMenuParentIds(parent_id uint) (parent_id_list []uint, err error) {
	var menu model.SysBaseMenu
	err = dao.DB.Select("parent_id").Where("id = ?", parent_id).First(&menu).Error
	if err != nil {
		return
	}
	parent_id_list = []uint{menu.ParentId, parent_id}
	return
}

func UpdateMenuInfo(menu model.SysBaseMenu) error {
	return dao.DB.Model(&model.SysBaseMenu{}).Where("id = ?", menu.ID).Updates(&model.SysBaseMenu{Name: menu.Name, Path: menu.Path, Icon: menu.Icon, Sort: menu.Sort, ParentId:menu.ParentId}).Error
}

func DeleteMenu(menu_id uint) error {
	return dao.DB.Where("id = ?", menu_id).Unscoped().Delete(&model.SysBaseMenu{}).Error
}
